<?php
define('IN_ADMINCP', TRUE);
define('IN_TESTCP', TRUE);
define('TESTCP_AJAX', AJAX);
define('TESTCP_PATH', dirname(__FILE__));
define('APP_PATH', dirname(TESTCP_PATH));
//echo TESTCP_PATH . '--' . APP_PATH;die;
require APP_PATH . '/app.php';
$admincp = new admincp;
$account = new admincp_account;
//上面的代码采用杨坤的框架，下面的代码采用原生PHP
include "center/Model.class.php";
include "center/Route.class.php";
class Index extends Model{
    public $show='';
    public $level1Data;
    public function __construct(){
        //用户权限认证
    }
    public function Action(){
        if(isset($_POST['submit'])){
            $levelArray=array(
                'level1'=>$_POST['level1']?$_POST['level1']:null,
                'level2'=>$_POST['level2']?$_POST['level2']:null,
                'level3'=>$_POST['level3']?$_POST['level3']:null,
            );
            $dataPage=$this->Page('oa_download','',1,$levelArray);
            $this->show=array(
                'dataPage'=>$dataPage,
            );
            return;
        }elseif(isset($_POST['searchbutton'])){
            $search=$_POST['search']? $_POST['search']:'';
            if(empty($search)){
                return;
            }
            $sql="SELECT * FROM oa_download WHERE name LIKE '%{$search}%'";
            return $this->show=array(
                'dataPage'=>array(
                    'numPage'=>1,//可以分多少也
                    'thisPage'=>parent::all($sql),//当前分页数据
                    'page'=>1,//当前分页
                    'num'=>1,
                ),
            );
        }else{
            //页面初始化数据
            $level1="SELECT * FROM oa_download_category WHERE level=1";
            $this->level1Data=parent::all($level1);
            $this->show=$this->Show();
        }
    }
    public function Show(){
        $getPage=isset($_GET['page'])? $_GET['page']: 1;
        $dataPage=$this->Page('oa_download',30,$getPage);
        return array(
            'dataPage'=>$dataPage,
        );
    }
    //from:表单  num:每一页显示数据条数  page:当前页码
    public function Page($from,$num,$page,$levelArray=null){
        //一共可以分多少页面
        if($levelArray!=null){
            //使用查询
            $where='';
            $i=1;
            foreach($levelArray as $k=>$v){
                if(!empty($v) && $i==1){
                    $where.=$k.'='.$v;
                }elseif(!empty($v)){
                    $where.=' AND '.$k.'='.$v;
                }
                $i++;
            }
            $thisSql="SELECT *
                        FROM $from
                       WHERE $where
                       ";
            //echo $thisSql;exit;
            $thisPage=parent::all($thisSql);
            //var_dump($thisPage);exit;
            return array(
                'numPage'=>1,//可以分多少也
                'thisPage'=>$thisPage,//当前分页数据
                'page'=>1,//当前分页
                'num'=>$num,
            );
        }
        //不使用查询
        $numSql="SELECT COUNT(*) FROM $from";
        $numPage=ceil((parent::total($numSql))/$num);
        $limit=(($page-1)*$num).','.$num;
        $thisSql="SELECT * FROM $from ORDER BY id DESC LIMIT $limit";
        $thisPage=parent::all($thisSql);
        return array(
            'numPage'=>$numPage,//可以分多少也
            'thisPage'=>$thisPage,//当前分页数据
            'page'=>$page,//当前分页
            'num'=>$num,
            );
    }
}
$model=new Index();
$model->Action();
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <link rel="stylesheet" type="text/css" href="assets/css/show.css"/>
    <script type="text/javascript" src="assets/js/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="content">
    <div id="content-box">
        <div class="content-head">
            <div class="redirect">
                <a>新闻&nbsp;+</a>
            </div>
            <div class="keyboard padding-l20">
                <form method="post" action="">
                    <input type="text" name="search" class="search padding-l40 color-black" placeholder="站内搜索"/>
                    <input type="submit" name="searchbutton" class="searchbutton" value="搜索"/>
                </form>
            </div>
            <form method="post" action="">
                <?php if(!empty($model->level1Data)):?>
                <div class="state">
                    <div class="float-l">
                        业务方向：
                    </div>
                    <input type="hidden" name='level1' value=""/>
                    <?php foreach($model->level1Data as $k=>$v):?>
                        <p class="state-radio state-off" data-cid="<?= $v['cid']; ?>"></p>
                        <p class="lable-p"><?= $v['name'];?></p>
                    <?php endforeach;?>
                </div>
                <?php endif;?>
                <div class="state" id="level2" style="display: none;">
                </div>
                <div class="state" id="level3" style="display: none;">
                </div>
                <input type="submit" name="submit" value="查询" style="height:35px; width:90px; margin:10px 20px; background:#1D923A; color: #ffffff;"/>
                <a href="<?= Route::Url('show');?>" style="margin:0 0 20px 0;line-height:35px; text-align: center; width:90px; background:#e55845; color: #ffffff; display: inline-block;">全部</a>
            </form>
        </div>
        <div class="content">
            <div class="box-head">
                <span class="width4">排序</span>
                <span class="width18">标题</span>
                <span class="width9">作者</span>
                <span class="width40">URL</span>
                <span class="width9">密码</span>
                <span class="width14">创建时间</span>
            </div>
            <ul id="list">
                <?php if($model->show['dataPage']):?>
                    <?php foreach($model->show['dataPage']['thisPage']as $k=>$v):?>
                        <li>
                            <p class="width4"><?= ($model->show['dataPage']['page']-1)*$model->show['dataPage']['num']+$k+1; ?></p>
                            <p class="width18"><?= $v['name'];?></p>
                            <p class="width9 red"><?= $v['admin'];?></p>
                            <p class="width40"><a href="<?= $v['url'];?>" target="_blank">点击下载</a></p>
                            <p class="width12"><?= empty($v['password'])?'无':$v['password'];?></p>
                            <p class="width14"><?= date('Y-m-d',$v['create_tim'])?></p>
                        </li>
                    <?php endforeach;?>
                <?php endif;?>
            </ul>
            <div class="box-footer">
                <ul class="page float-l">
                    <li><a class="page-l"></a></li>
                    <?php for($i=1;$i<=$model->show['dataPage']['numPage'];$i++):?>
                        <?php if($i==$model->show['dataPage']['page']):?>
                            <li><a href="<?= Route::Url('show',['page'=>$i]);?>" class="page-n page-click"><?= $i;?></a></li>
                        <?php else:?>
                            <li><a href="<?= Route::Url('show',['page'=>$i]);?>" class="page-n"><?= $i;?></a></li>
                        <?php endif;?>
                    <?php endfor;?>
                    <li><a class="page-r"></a></li>
                </ul>

            </div>
        </div>
    </div>
</div>
<script>
    //单选框点击样式,这里目前使用面向对象的写法，虽然简单但是过程不好理解，以后将使用foreach遍历的方法
    (function($){
        //这里使用了实践冒泡，事件代理
        $('#content-box').on('click','.state-radio', function(){
            //改变同级别元素的样式
            $(this).siblings('.state-radio').removeClass('state-on state-off');
            $(this).siblings('.state-radio').addClass('state-off');
            //改变自身的样式
            $(this).removeClass('state-off');
            $(this).addClass('state-on');
            //获取当前分类的id，并设置input的值
            var cid=$(this).data('cid');
            $(this).siblings('input').val(cid);
            //判断是否已经到达第三级目录，如果到达第三级目录就不需要请求数据
            if($(this).siblings('input').attr('name')=='level3'){
                return;
            }
            $.ajax({
                type:'post',
                url:'<?= Route::Url('edit'); ?>',
                data:{cid:cid},
                success:function(response,status,xhr){
                    //debugger;
                    var ajaxs=JSON.parse(response);
                    if(ajaxs[0]!==undefined && ajaxs[0]['level']==2){
                        var htmls=createHtml("文件类型：",ajaxs,2);
                        $('#level2').css('display','block');
                        $('#level2').html(htmls);
                        $('#level3').css('display','none');
                        $('#level3').html('');
                    }
                    if(ajaxs[0]!==undefined && ajaxs[0]['level']==3){
                        var htmls=createHtml("产品分类：",ajaxs,3);
                        $('#level3').css('display','block');
                        $('#level3').html(htmls);
                    }else{
                        $('#level3').css('display','none');
                        $('#level3').html('');
                    }

                    console.log(ajaxs);
                }
            });
            function createHtml(info,ajaxs,level){
                var htmls='';
                htmls+="<div class='float-l'>"+info+"</div>";
                htmls+="<input type='hidden' name='level"+level+"' value='' />";
                for(var i=0;i<ajaxs.length;i++){
                    htmls+="<p class='state-radio state-off' data-cid='"+ajaxs[i]['cid']+"'></p>";
                    htmls+="<p class='lable-p'>"+ajaxs[i]['name']+"</p>";
                }
                return htmls;
            }
        });
    })(jQuery)
</script>
</body>
</html>